# -*- coding: utf-8 -*-
"""
简化版红楼梦人物关系网络图
"""

from pyecharts import options as opts
from pyecharts.charts import Graph

# 红楼梦核心人物关系数据（简化）
# 节点数据 - 主要人物
nodes = [
    {"name": "贾宝玉", "symbolSize": 30, "category": 0},
    {"name": "林黛玉", "symbolSize": 25, "category": 0},
    {"name": "薛宝钗", "symbolSize": 25, "category": 0},
    {"name": "王熙凤", "symbolSize": 20, "category": 1},
    {"name": "贾母", "symbolSize": 20, "category": 2},
    {"name": "贾政", "symbolSize": 18, "category": 2},
    {"name": "王夫人", "symbolSize": 18, "category": 2},
    {"name": "袭人", "symbolSize": 15, "category": 3},
    {"name": "晴雯", "symbolSize": 15, "category": 3},
    {"name": "紫鹃", "symbolSize": 15, "category": 3},
]

# 边数据 - 人物关系
links = [
    {"source": "贾宝玉", "target": "林黛玉", "value": 8},
    {"source": "贾宝玉", "target": "薛宝钗", "value": 7},
    {"source": "林黛玉", "target": "薛宝钗", "value": 6},
    {"source": "贾宝玉", "target": "袭人", "value": 7},
    {"source": "贾宝玉", "target": "晴雯", "value": 6},
    {"source": "林黛玉", "target": "紫鹃", "value": 8},
    {"source": "王熙凤", "target": "贾母", "value": 6},
    {"source": "贾政", "target": "贾宝玉", "value": 6},
    {"source": "王夫人", "target": "贾宝玉", "value": 6},
    {"source": "贾母", "target": "贾宝玉", "value": 7},
    {"source": "贾政", "target": "王夫人", "value": 5},
    {"source": "贾政", "target": "贾母", "value": 5},
    {"source": "王夫人", "target": "贾母", "value": 5},
    {"source": "王熙凤", "target": "贾琏", "value": 7},
]

# 类别样式 - 用于区分不同类型的人物
categories = [
    {"name": "主角"},
    {"name": "管家"},
    {"name": "长辈"},
    {"name": "丫鬟"},
]

def create_simple_network():
    """创建简化的人物关系网络图"""
    graph = (
        Graph(init_opts=opts.InitOpts(width="1000px", height="600px"))
        .add(
            "",
            nodes,
            links,
            categories=categories,
            repulsion=4000,
            edge_length=150,
            gravity=0.1,
            friction=0.6,
            linestyle_opts=opts.LineStyleOpts(curve=0.3, width=2),
            label_opts=opts.LabelOpts(is_show=True, font_size=14)
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="《红楼梦》核心人物关系图"),
            legend_opts=opts.LegendOpts(orient="vertical", pos_left="2%", pos_top="15%"),
            tooltip_opts=opts.TooltipOpts(formatter="{b} - {c}")
        )
    )
    return graph

def main():
    # 创建并保存简化版人物关系网络图
    graph = create_simple_network()
    graph.render("红楼梦简化人物关系图.html")
    print("简化版人物关系图已保存为 红楼梦简化人物关系图.html")
    
    # 输出说明文档
    with open("红楼梦简化人物关系图说明.txt", "w", encoding="utf-8") as f:
        f.write("《红楼梦》简化人物关系图说明\n")
        f.write("=" * 30 + "\n\n")
        
        f.write("一、图例说明\n")
        f.write("1. 节点大小：代表人物在故事中的重要性\n")
        f.write("2. 连线粗细：代表人物之间关系的紧密程度\n")
        f.write("3. 颜色分类：\n")
        f.write("   - 蓝色：主角（贾宝玉、林黛玉、薛宝钗）\n")
        f.write("   - 绿色：管家（王熙凤等）\n")
        f.write("   - 紫色：长辈（贾母、贾政、王夫人等）\n")
        f.write("   - 浅蓝色：丫鬟（袭人、晴雯、紫鹃等）\n\n")
        
        f.write("二、交互操作\n")
        f.write("1. 鼠标悬停：查看人物名称和关系强度\n")
        f.write("2. 鼠标拖拽：移动节点位置\n")
        f.write("3. 滚轮缩放：放大缩小视图\n")
        f.write("4. 点击图例：显示/隐藏对应类别的人物\n\n")
        
        f.write("三、主要人物关系\n")
        f.write("1. 贾宝玉-林黛玉：青梅竹马，情感深厚\n")
        f.write("2. 贾宝玉-薛宝钗：金玉良缘，家族安排\n")
        f.write("3. 贾宝玉-袭人：主仆关系，情感依赖\n")
        f.write("4. 林黛玉-紫鹃：主仆情深，如姐妹般\n")
        f.write("5. 贾政-贾宝玉：父子关系，期望与叛逆\n")
        f.write("6. 贾母-贾宝玉：祖孙情深，宠爱有加\n")
    
    print("说明文档已保存为 红楼梦简化人物关系图说明.txt")

if __name__ == "__main__":
    main()